package com.cfs.config.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import com.cfs.custom.enums.EnumAccountSrc;

/**
 * Created by Administrator on 2017/9/26/026.
 */
@MappedJdbcTypes({JdbcType.VARCHAR})
@MappedTypes(EnumAccountSrc.class)
public class EnumSrcTypeHandler implements TypeHandler<EnumAccountSrc> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, EnumAccountSrc enumAccountSrc, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, enumAccountSrc.toString());
    }

    @Override
    public EnumAccountSrc getResult(ResultSet resultSet, String s) throws SQLException {
        String val = resultSet.getString(s);
        if (val != null) {
            return EnumAccountSrc.valueOf(val);
        }
        return null;
    }

    @Override
    public EnumAccountSrc getResult(ResultSet resultSet, int i) throws SQLException {
        String val = resultSet.getString(i);
        if (val != null) {
            return EnumAccountSrc.valueOf(val);
        }
        return null;
    }

    @Override
    public EnumAccountSrc getResult(CallableStatement callableStatement, int i) throws SQLException {
        String val = callableStatement.getString(i);
        if (val != null) {
            return EnumAccountSrc.valueOf(val);
        }
        return null;
    }
}
